﻿
@{
    ViewBag.Title = "执行补片";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";

    var fragments = ViewBag.FragmentTypes as List<KeyValuePair<int, string>>;
    var processTypes = ViewBag.CompenProcessTypes as List<KeyValuePair<int, string>>;
    var user = ViewBag.User as IOA.MES.DataAccess.VModel.VUser;
    var workshops = ViewBag.Workshops as List<IOA.MES.DataAccess.VModel.VWorkshop>;
    var workOrderNos = ViewBag.WorkOrderNos as List<string>;
    var duties = ViewBag.Duties as List<IOA.MES.DataAccess.VModel.VWorkshopDuty>;
    var cells = ViewBag.Cells as List<string>;
    var startTime = DateTime.Now.TimeOfDay < new TimeSpan(8, 0, 0) ? DateTime.Today.AddDays(-1).AddHours(20) : (DateTime.Now.TimeOfDay < new TimeSpan(20, 0, 0) ? DateTime.Today.AddHours(8) : DateTime.Today.AddHours(20));
    var endTime = DateTime.Now.TimeOfDay < new TimeSpan(8, 0, 0) ? DateTime.Today.AddHours(8) : (DateTime.Now.TimeOfDay < new TimeSpan(20, 0, 0) ? DateTime.Today.AddHours(20) : DateTime.Today.AddDays(1).AddHours(8));
}

@section Contents{
    <link href="~/res/kendo/styles/kendo.common.min.css" rel="stylesheet" />
    <link href="~/res/kendo/styles/kendo.material.min.css" rel="stylesheet" />
    <style>
        .layui-form-label {
            width: 100px;
        }

        #txtCellCode, #txtBatchNo {
            height: 30px;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid layui-form">
        <div class="layui-card">
            <div class="layui-card-header">
                电池片信息
            </div>
            <div class="layui-form layui-card-body layuiadmin-card-header-auto">
                <div class="layui-row">
                    <div class="layui-col-xs4 layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkShop">生产车间：</label>
                        <div class="layui-input-inline">
                            <select name="WorkshopCode" lay-filter="WorkshopCode">
                                @foreach (var item in workshops)
                                {
                                    <option value="@item.WorkshopCode">@item.WorkshopName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-col-xs4 layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.WorkorderNo">工单号：</label>
                        <div class="layui-input-inline">
                            <select name="WorkOrderNo" lay-search lay-filter="WorkOrderNo">
                                <option value="">请选择</option>
                                @foreach (var item in workOrderNos)
                                {
                                    <option value="@item">@item</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-col-xs4 layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.Operator">补片人：</label>
                        <div class="layui-input-inline">
                            <input type="text" value="@user.TrueName" readonly class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-xs8 layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.Date">补片日期：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtRangeTime" name="TimeRange" style="width:350px;float:left;" value="@(startTime.ToString("yyyy-MM-dd HH:mm:ss") + " 至 " + endTime.ToString("yyyy-MM-dd HH:mm:ss"))" autocomplete="off" class="layui-input">
                            <button class="layui-btn layuiadmin-btn-list layui-bg-cyan btnPreDuty" type="button" style="margin-left:10px;">
                                <i class="layui-icon layui-icon-prev layuiadmin-button-btn"></i>
                            </button>
                            <button class="layui-btn layuiadmin-btn-list layui-bg-cyan btnNextDuty" type="button">
                                <i class="layui-icon layui-icon-next layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                    <div class="layui-col-xs4 layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.Duty">补片班次：</label>
                        <div class="layui-input-inline">
                            <select name="CompenDutyId" lay-filter="CompenDutyId">
                                <option value="">请选择</option>
                                @foreach (var item in duties)
                                {
                                    <option value="@item.PkId">@item.DutyName</option>
                                }
                            </select>
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-xs4 layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.CompenProcess">补片工序：</label>
                        <div class="layui-input-inline">
                            <select name="CompenProcess" lay-filter="CompenProcess">
                                <option value="">请选择</option>
                                @foreach (var item in processTypes)
                                {
                                    <option value="@item.Key">@item.Value</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-col-xs4 layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.CompenReason">补片原因：</label>
                        <div class="layui-input-inline">
                            <select name="CompenReason" lay-filter="CompenReason">
                                <option value="">请选择</option>
                                @foreach (var item in fragments)
                                {
                                    <option value="@item.Key">@item.Value</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-col-xs4 layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.CompenCount">补片数量：</label>
                        <div class="layui-input-inline">
                            <input type="number" name="CellCount" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-card">
            <div class="layui-card-header">
                补片信息
            </div>
            <div class="layui-form layui-card-body layuiadmin-card-header-auto">
                <div class="layui-row">
                    <div class="layui-col-xs4 layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.CellCode">电池料号：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtCellCode" placeholder="请输入电池料号" style="width:100%;" value="">
                        </div>
                    </div>
                    <div class="layui-col-xs8 layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.BatchNo">批次号：</label>
                        <div class="layui-input-inline" style="width:80%;">
                            <input type="text" id="txtBatchNo" placeholder="请输入批次号" style="width:90%;" value="">
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-xs4 layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.CellSpecs">电池规格：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="CellSpecs" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-xs4 layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.CellFactory">电池厂家：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="CellFactory" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-xs4 layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.CellCrystalCategory">晶体类别：</label>
                        <div class="layui-input-inline">
                            <input type="text" name="CellCrystalType" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-row">
                    <div class="layui-col-xs4 layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.CellEfficiency">电池效率：</label>
                        <div class="layui-input-inline">
                            <input type="number" name="CellEfficiency" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-xs4 layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.CellArea">单片面积：</label>
                        <div class="layui-input-inline">
                            <input type="number" name="CellArea" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-xs4 layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.CellPower">单片功率：</label>
                        <div class="layui-input-inline">
                            <input type="number" name="CellSinglePower" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-card">
            <div class="layui-form layui-card-body layuiadmin-card-header-auto">
                <div class="layui-row">
                    <div class="layui-col-xs4 layui-inline">
                        <label class="layui-form-label"></label>
                        <div class="layui-input-inline">
                            <button class="layui-btn layui-btn-normal" lay-filter="submit" lay-submit>确认补片</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</script>

@section Scripts{
    <script src="/res/kendo/kendo.all.min.js"></script>
    <script src="/res/kendo/js/cultures/kendo.culture.zh-CN.min.js"></script>
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer', 'form', 'element', 'laydate'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    laydate = layui.laydate,
                    form = layui.form;
                laydate.render({
                    elem: '#txtRangeTime'
                    , type: 'datetime'
                    , rigger: 'click'
                    , range: '至'
                });
                var txtCellCode = $("#txtCellCode").kendoAutoComplete({
                    dataSource: [],
                    filter: "contains",
                    placeholder: "请输入电池料号",
                    select: cellSelected
                }).focus(function () {
                    txtCellCode.search("");
                }).data("kendoAutoComplete"),
                    txtBatchNo = $("#txtBatchNo").kendoAutoComplete({
                        dataSource: [],
                        filter: "contains",
                        placeholder: "选择或输入批次号",
                        select: batchSelected
                    }).focus(function () {
                        txtBatchNo.search("");
                    }).data("kendoAutoComplete");

                form.on("select(WorkshopCode)", function (data) {
                    $("select[name=WorkOrderNo]").html("<option value=''>请选择</option>");
                    txtCellCode.setDataSource(new kendo.data.DataSource({ data: [] }));
                    txtBatchNo.setDataSource(new kendo.data.DataSource({ data: [] }));
                    txtCellCode.value("");
                    txtBatchNo.value("");

                    $.ajax({
                        url: "/WorkOrder/QueryWorkshopWorkOrderNos",
                        data: {
                            workshopCode: data.value
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            var html = ["<option value=''>请选择</option>"];
                            for (var i = 0; i < res.Data.length; i++) {
                                html.push("<option value='" + res.Data[i] + "'>" + res.Data[i] + "</option>");
                            }
                            $("select[name=WorkOrderNo]").html(html.join("\n"));
                            form.render();
                        }
                    });

                    $.ajax({
                        url: "/WorkOrder/QueryWorkshopDuties",
                        data: {
                            workshopCode: data.value
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            var html = ["<option value=''>请选择</option>"];
                            for (var i = 0; i < res.Data.length; i++) {
                                html.push("<option value='" + res.Data[i].PkId + "'>" + res.Data[i].DutyName + "</option>");
                            }
                            $("select[name=CompenDutyId]").html(html.join("\n"));
                            form.render();
                        }
                    });
                });

                var allocateProducts = new Array();
                form.on("select(WorkOrderNo)", function (data) {
                    txtCellCode.setDataSource(new kendo.data.DataSource({ data: [] }));
                    txtBatchNo.setDataSource(new kendo.data.DataSource({ data: [] }));
                    txtCellCode.value("");
                    txtBatchNo.value("");
                    $.ajax({
                        url: "/WorkOrder/QueryWorkOrderCells",
                        data: {
                            workOrderNo: data.value
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                allocateProducts = new Array();
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            allocateProducts = res.Data;
                            var productCodes = allocateProducts.map((item) => item.ProductCode);
                            for (var i = productCodes.length - 1; i >= 0; i--) {
                                if (productCodes.filter((item) => { return item == productCodes[i]; }).length > 1) {
                                    productCodes.splice(i, 1);
                                }
                            }
                            txtCellCode.setDataSource(new kendo.data.DataSource({ data: productCodes }));
                        }
                    });
                });
                function cellSelected(e) {
                    var batchNos = allocateProducts.filter((item) => {
                        return item.ProductCode == e.dataItem && item.BatchNo != '';
                    }).map((item) => item.BatchNo);
                    txtBatchNo.setDataSource(new kendo.data.DataSource({
                        data: batchNos
                    }));
                    txtBatchNo.value("");
                }
                function batchSelected(e) {
                    var allcateProduct = allocateProducts.find((item) => {
                        return item.BatchNo == e.dataItem;
                    });
                    $("input[name=CellSpecs]").val(allcateProduct == null ? "" : allcateProduct.Specifications);
                    $("input[name=CellFactory]").val(allcateProduct == null ? "" : allcateProduct.ProductionCompany);
                }

                form.on('submit(submit)', function (data) {
                    var field = data.field;
                    field.CellCode = txtCellCode.value() ?? "";
                    field.CellBatchNo = txtBatchNo.value() ?? "";
                    if (field.WorkOrderNo.trim() == '') {
                        return layer.msg("请选择工单号", { icon: 5 });
                    } else if (field.TimeRange == "") {
                        return layer.msg("请选择补片日期", { icon: 5 });
                    } else if (field.CompenDutyId == "") {
                        return layer.msg("请选择补片班次", { icon: 5 });
                    } else if (field.CompenProcess == "") {
                        return layer.msg("请选择补片工序", { icon: 5 });
                    } else if (field.CompenReason == "") {
                        return layer.msg("请选择补片原因", { icon: 5 });
                    } else if (field.CellCount.trim() == "") {
                        return layer.msg("请输入补片数量", { icon: 5 });
                    } else if (field.CellCode == "") {
                        return layer.msg("请选择电池料号", { icon: 5 });
                    } else if (field.CellBatchNo.trim() == "") {
                        return layer.msg("请选择电池批次号", { icon: 5 });
                    }
                    field.StartTime = field.TimeRange.split('至')[0].trim();
                    field.EndTime = field.TimeRange.split('至')[1].trim();
                    field.CompenProcess = parseInt(field.CompenProcess);
                    field.CompenReason = parseInt(field.CompenReason);
                    field.CellCount = parseInt(field.CellCount.trim());
                    submit(field);
                    return false;
                });
                function submit(data) {
                    var loadId = layer.load();
                    $.ajax({
                        url: "/WorkOrder/DoCompenCell",
                        data: {
                            model: data
                        },
                        complete: function () {
                            layer.close(loadId);
                        },
                        success: function (res) {
                            if (res.ErrCode != 200) {
                                layer.msg(res.ErrMsg, { icon: 5 });
                                return;
                            }
                            window.parent.layui.layer.msg("补片成功！", { icon: 6 });
                            location.reload();
                        }
                    });
                }
            });
        }
    </script>
}
